/* 
 * File:   Extractor.cpp
 * Author: guilhermemg
 * 
 * Created on 19 de Fevereiro de 2012, 18:44
 */

#include "Extractor.h"

//Extractor* Extractor::uniqueInstance;

Extractor::Extractor(vector<string> imgs, string pathT) {
    setImagesPaths(imgs);
    setPathTable(pathT);
}

Extractor::Extractor(const Extractor& orig) {
}

Extractor::~Extractor() {
}

void Extractor::run() {
    AveragePixelIntensity alg1;
    AveragePixelSaturation alg3;
    AveragePixelHue alg4;
    RuleOfThirdsBasedOnHue alg5;
    RuleOfThirdsBasedOnSaturation alg6;
    RuleOfThirdsBasedOnValue alg7;
    SizeFeature alg22;
    AspectRatio alg23;
    
    algorithms.push_back(&alg1);
    algorithms.push_back(&alg3);
    algorithms.push_back(&alg4);
    algorithms.push_back(&alg5);
    algorithms.push_back(&alg6);
    algorithms.push_back(&alg7);
    algorithms.push_back(&alg22);
    algorithms.push_back(&alg23);
    
    Table table(getPathTable());
    vector<string> fields;
    //vector<string> legend;
    for(int i = 0; i < algorithms.size(); i++) {
        fields.push_back(algorithms.at(i)->getLargeName());
        //legend.push_back(algorithms.at(i)->getName());
    }
    
    //table.writeLegend(legend);
    table.writeFields(fields);
    
    for(int i = 0; i < this->imagesPaths.size(); i++) {
        IplImage* img = cvLoadImage(imagesPaths.at(i).c_str());
        IplImage* imgConverted = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 3); // bag of vectors

        // converte imagem para hsv
        cvCvtColor(img, imgConverted, CV_BGR2HSV);
        vector<double> values;
        for(int j = 0; j < this->algorithms.size(); j++) {
            algorithms.at(j)->setImage(imgConverted);
            algorithms.at(j)->calculates();
            //cout << "image" << i << ": " << algorithms.at(j)->toString() << endl; 
            values.push_back(algorithms.at(j)->getValue());
        }
        cvReleaseImage(&imgConverted);
        cvReleaseImage(&img);
        table.writeLineWithValues(values, i);
        //cout << "" << endl;
    }
    
    
}

void Extractor::setPathTable(string pathT1) {
    this->pathT = pathT1;
}

string Extractor::getPathTable() {
    return this->pathT;
}

void Extractor::setImagesPaths(vector<string> imgsPaths) {
    this->imagesPaths = imgsPaths;
}

vector<string> Extractor::getImagesPaths() {
    return this->imagesPaths;
}

//Extractor* Extractor::getInstance(vector<IplImage*> imgs, string pathTable) {
//    if(Extractor::uniqueInstance == NULL) {
//        Extractor::uniqueInstance = new Extractor(imgs, pathTable);
//    }
//    else
//        return Extractor::uniqueInstance;
//}